using System;
using System.Collections;
using SecuBat.Common;

namespace SecuBat.Crawling
{
	/// <summary>
	/// Summary description for WebAddressCollection.
	/// </summary>
	public class WebAddressCollection : SharedData, IDictionary, IEnumerable, ICollection
	{
		private Hashtable _table;
		public WebAddressCollection()
		{
			_table = new Hashtable();
		}

		public void CopyTo(Array array, int index)
		{
			ReadLock();
			_table.CopyTo(array, index);
			ReadRelease();
		}

		public int Count
		{
			get
			{
				ReadLock();
				int c = _table.Count;
				ReadRelease();
				return c;
			}
		}

		public object SyncRoot
		{
			get
			{
				ReadLock();
				object o = _table.SyncRoot;
				ReadRelease();
				return o;
			}
		}

		public bool IsSynchronized
		{
			get
			{
				ReadLock();
				bool s = _table.IsSynchronized;
				ReadRelease();
				return s;
			}
		}

		public bool Contains(WebAddress address)
		{
			ReadLock();
			bool b = _table.Contains(address.Url);
			ReadRelease();
			return b;
		}

		public bool Contains(object key)
		{
			ReadLock();
			bool b = _table.Contains(key);
			ReadRelease();
			return b;
		}

		public void Add(object key, object value)
		{
			WriteLock();
			if (!_table.Contains(key))
				_table.Add(key, value);
			WriteRelease();
		}

		public void Add(WebAddress address)
		{
			WriteLock();
			if (address != null)
				this.Add(address.Url, address);
			WriteRelease();
		}

		public void Add(WebAddress[] array)
		{
			WriteLock();
			foreach(WebAddress a in array)
				this.Add(a);
			WriteRelease();
		}

		public void Add(WebAddressCollection coll)
		{
			WriteLock();
			foreach(WebAddress a in coll.Values)
				this.Add(a);
			WriteRelease();
		}

		public void Clear()
		{
			WriteLock();
			_table.Clear();
			WriteRelease();
		}

		IDictionaryEnumerator System.Collections.IDictionary.GetEnumerator()
		{
			ReadLock();
			IDictionaryEnumerator e = _table.GetEnumerator();
			ReadRelease();
			return e;
		}

		IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			ReadLock();
			IEnumerator e = _table.GetEnumerator();
			ReadRelease();
			return e;
		}

		public void Remove(object key)
		{
			WriteLock();
			_table.Remove(key);
			WriteRelease();
		}

		public ICollection Keys
		{
			get
			{
				ReadLock();
				ICollection k = _table.Keys;
				ReadRelease();
				return k;
			}
		}

		public ICollection Values
		{
			get
			{
				ReadLock();
				ICollection v = _table.Values;
				ReadRelease();
				return v;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				ReadLock();
				bool i = _table.IsReadOnly;
				ReadRelease();
				return i;
			}
		}

		public bool IsFixedSize
		{
			get
			{
				ReadLock();
				bool f = _table.IsFixedSize;
				ReadRelease();
				return f;
			}
		}

		public object this[object key]
		{
			get
			{
				ReadLock();
				object o = _table[key];
				ReadRelease();
				return o;
			}
			set
			{
				WriteLock();
				_table[key] = value;
				WriteRelease();
			}
		}
	}
}
